import request from '@/utils/request'

// 通用读取数据
export function readFileData(filePath,fileSeek,byteLength) {
    return request({
        responseType:"arraybuffer",
        url: '/common/readFileData',
        method: 'get',
        params: {filePath:filePath,fileSeek:fileSeek,byteLength:byteLength}
    })
}

export function readImgData(filePath) {
    return request({
        url: '/common/readImgData',
        method: 'get',
        params: {filePath:filePath}
    })
}

export function Int16Array2Float32Array (int16Array) {
    const data = int16Array
    let len = data.length
    let i = 0
    const dataAsFloat32Array = new Float32Array(len)

    while (i < len) {
        dataAsFloat32Array[i] = data[i++]
    }
    return dataAsFloat32Array
}

export function Int8Array2Int16Array (int8Array) {
    let int16Array = new Int16Array(int8Array.length / 2)
    for (let i = 0; i < int8Array.length; i += 2) {
        let int16 = (int8Array[i + 1] << 8) | (int8Array[i] & 0xff)
        int16Array[i / 2] = int16
    }
    return int16Array
}

//IEEE754
function hex2float(num) {
    //符号位
    var sign = (num & 0x80000000) ? -1 : 1;
    //指数位
    var exponent = ((num >> 23) & 0xff) - 127;
    //尾数位
    var mantissa = 1 + ((num & 0x7fffff) / 0x7fffff);
    return sign * mantissa * Math.pow(2, exponent);
}

export function Int8Array2Float32Array (int8Array) {
    let float32Array = new Float32Array(int8Array.length / 4)
    for (let i = 0; i < int8Array.length; i += 4) {
        var mfloat = ((int8Array[i+0] & 0xFF) << 24) |
            ((int8Array[i+1] & 0xFF) << 16) |
            ((int8Array[i+2] & 0xFF) << 8) |
            (int8Array[i+3] & 0xFF);
        float32Array[i / 4] = hex2float(mfloat)
    }
    return float32Array
}

export function MaxAndMin (arr) {
    let nowArr = [];
    for (let i=0;i < arr.length;i++){
        nowArr.push(arr[i]);
    }
    nowArr.sort((a, b) => {return a - b})
    return [nowArr[0], nowArr[nowArr.length - 1]]
}